/* 
    file:   ui_draw_img.c
 */
/* ------------------------------------------------------------------------- */
#include "ui_draw_img.h"
#include "stdio.h"


void ui_draw_img_a(ui_screen_t * pscr,struct ui_obj * obj,uint8_t chl,uint32_t color)
{
    int ix,iy;
    
    uint16_t * disbuf = pscr->pbfb;

    if(chl)
    {
        disbuf = pscr->pbfa;
    }

    uint32_t dwidth = pscr->width;

    lv_img_dsc_t * pimg;
    uint8_t * pimgbuf;
    uint32_t imh;
    uint32_t imw;


    int sx = 0;
    int sy = 0;
    point realpoint;

    int bx,by;

    pimg = *(lv_img_dsc_t **)(obj->objdat);
    pimgbuf = (uint8_t *)pimg->data;
    imh = pimg->header.h;
    imw = pimg->header.w;
    

    ui_getlct(obj,&realpoint);
    sx = obj->area.x2-obj->area.x1;
    sy = obj->area.y2-obj->area.y1;

    if(sx>imw)
    {
        bx = (sx-imw)/2;
        sx = imw;
    }
    else
    {
        bx = 0;
    }
    

    if(sy>imh)
    {
        by = (sy-imh)/2;
        sy = imh;
    }
    else
    {
        by = 0;
    }
    

    if (pimg->header.cf == LV_IMG_CF_TRUE_COLOR)
    {
        for(iy=0;iy<sy;iy++)
        {
           
            uint8_t *pof = (uint8_t *)&((uint16_t *)disbuf)[(realpoint.y+iy+by)*dwidth+realpoint.x+bx];
            uint8_t *pif = (uint8_t *)&pimgbuf[iy*imw*2];
            if(((realpoint.y+iy+by)*dwidth+realpoint.x+bx)<(pscr->width*pscr->heigh))
            {
                for(ix=0;ix<sx;ix++)
                {
                    pof[ix*2] = pif[ix*2];
                    pof[ix*2+1] = pif[ix*2+1];
                }
            }
        }
    }
    if (pimg->header.cf == LV_IMG_CF_TRUE_COLOR_ALPHA)
    {
        uint16_t colorback;
        for(iy=0;iy<sy;iy++)
        {
            
            uint16_t *pof = &((uint16_t *)disbuf)[(realpoint.y+iy+by)*dwidth+realpoint.x+bx];
            uint8_t *pif = (uint8_t *)&pimgbuf[iy*imw*3];
            if(((realpoint.y+iy+by)*dwidth+realpoint.x+bx)<(pscr->width*pscr->heigh))
            {
                for(ix=0;ix<sx;ix++)
                {
                    colorback = (pif[ix*3+1]<<8)|(pif[ix*3]);
                    pof[ix] = AlphaColor565(colorback,pof[ix],pif[ix*3+2]);
                }
            }
        }
    }

    if(pimg->header.cf == LV_IMG_CF_ALPHA_8BIT)
    {
        for(iy=0;iy<sy;iy++)
        {
            uint16_t *pof = &((uint16_t *)disbuf)[(realpoint.y+iy+by)*dwidth+realpoint.x+bx];
            uint8_t *pif = &((uint8_t *)pimgbuf)[iy*imw];
            if(((realpoint.y+iy+by)*dwidth+realpoint.x+bx)<(pscr->width*pscr->heigh))
            {
                for(ix=0;ix<sx;ix++)
                {
                    pof[ix] = AlphaColor565(color,pof[ix],pif[ix]);
                }
            }
        }
    }
}

void ui_draw_img_b(ui_screen_t * pscr,uint8_t chl,lv_img_dsc_t * pimg,point * ppont,uint32_t color)
{
    int      sx = 0;
    int      sy = 0;
    int      ex = 0;
    int      ey = 0;

    int ix = 0;
    int iy = 0;

    uint16_t * disbuf = pscr->pbfb;
    uint8_t * pimgbuf;

    if(chl)
    {
        disbuf = pscr->pbfa;
    }
    
    uint32_t dwidth = pscr->width;//display buf's width
    uint32_t dheigh = pscr->heigh;//display buf's heigh

    pimgbuf = (uint8_t *)pimg->data;
    uint32_t imw = pimg->header.w; //img's width
    uint32_t imh = pimg->header.h; //img's heigh
    
    sx = ui_int_limit(ppont->x,0,dwidth-1);
    sy = ui_int_limit(ppont->y,0,dheigh-1);
    ex = ui_int_limit(sx+imw-1,0,dwidth-1);
    ey = ui_int_limit(sy+imh-1,0,dheigh-1);

    //printf("img:%d,%d,%d,%d,%d,%d\r\n",sx,sy,ex,ey,dwidth,dwidth);
    if (pimg->header.cf == LV_IMG_CF_TRUE_COLOR)
    {
        for(iy=sy;iy<=ey;iy++)
        {
           
            uint8_t *pof = (uint8_t *)(&disbuf[iy*dwidth+sx]);
            uint8_t *pif = &pimgbuf[(iy-sy)*imw*2];
            
            for(ix=sx;ix<=ex;ix++)
            {
                pof[ix*2] = pif[ix*2];
                pof[ix*2+1] = pif[(ix-sx)*2+1];
            }
        }
    }
    
    if (pimg->header.cf == LV_IMG_CF_TRUE_COLOR_ALPHA)
    {
        uint16_t colorback;
        for(iy=sy;iy<=ey;iy++)
        {
            uint16_t *pof = &disbuf[iy*dwidth+sx];
            uint8_t *pif = &pimgbuf[(iy-sy)*imw*3];

            for(ix=sx;ix<=ex;ix++)
            {
                colorback = (pif[(ix-sx)*3+1]<<8)|(pif[(ix-sx)*3]);
                pof[ix] = AlphaColor565(colorback,pof[ix],pif[(ix-sx)*3+2]);
            }
        }
    }

    if(pimg->header.cf == LV_IMG_CF_ALPHA_8BIT)
    {
        for(iy=sy;iy<=ey;iy++)
        {
            uint16_t *pof = &disbuf[iy*dwidth+sx];
            uint8_t *pif = &pimgbuf[(iy-sy)*imw];

            for(ix=sx;ix<=ex;ix++)
            {
                pof[ix] = AlphaColor565(color,pof[ix],pif[(ix-sx)]);
            }
        }
    }
}



